package com.Lyoto.NetNotify.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author Lyoto
 * @Description 获取当前网络信息
 * @Date 2022-09-19  18:11
 * @Version
 **/
public class NetUtil {
    static final String OS_NAME = "os.name";
    
    public static String getCurrentIp() throws IOException {
        String ip = "";
        String ifConfig = "https://ifconfig.me";
        
        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(ifConfig);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
            ip = inputLine.toString().trim();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return ip;
    }
    public static boolean isLinux() {
        return System.getProperty(OS_NAME).contains(EPlatForm.Linux.getDescription());
    }
    
    public static boolean isWindows() {
        return System.getProperty(OS_NAME).contains(EPlatForm.Windows.getDescription());
    }
    
    private static String execCommand(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process ipconfig = runtime.exec(command);
        InputStream inputStream = ipconfig.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        String line = null;
        StringBuilder builder = new StringBuilder();
        while ((line = bufferedReader.readLine()) != null){
            builder.append(line);
        }
        return builder.toString();
    }
    }
    
